#!/usr/bin/python
# -*- coding: utf-8 -*-

from widget.tools import render_database_tree
from odbc_p.DB import loader

from PyQt4.QtGui import QTreeView, QLineEdit, QComboBox, QStandardItemModel

__author__ = 'xiwei'


class BaseHandler:
    def __init__(self, ui):
        """
            init
        Args:
            ui (UI.App): ui
        """
        self.ui = ui

        self.data = None
        self.db = None

        self.tree_view = QTreeView()
        self.tree_view_model = QStandardItemModel()

        self.host_edit = QLineEdit()
        self.port_edit = QLineEdit()
        self.type_comb = QComboBox()
        self.uid_edit = QLineEdit()
        self.pwd_edit = QLineEdit()

    def connect_info(self):
        connect_info = {
            'server': str(self.host_edit.text()),
            'port': str(self.port_edit.text()),
            'type': str(self.type_comb.currentText()),
            'uid': str(self.uid_edit.text()),
            'pwd': str(self.pwd_edit.text()),
        }
        return connect_info

    def connect(self):
        self.db = loader(self.connect_info())
        self.data = self.db.get_all_db_info()
        self.render_database_tree()

    def render_database_tree(self):
        if not (self.data or self.tree_view):
            return

        render_database_tree(self.tree_view_model, self.data)

